package fr.resasante.spring3.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import fr.resasante.exception.ApplicationException;
import fr.resasante.ldap.LdapSearch;
import fr.resasante.spring3.controller.common.ControllerAbstract;

@Controller
public class AjaxController extends ControllerAbstract {
	public String convertStreamToString(InputStream is) throws IOException {
		//
		// To convert the InputStream to String we use the
		// Reader.read(char[] buffer) method. We iterate until the
		// Reader return -1 which means there's no more data to
		// read. We use the StringWriter class to produce the string.
		//
		if (is != null) {
			final Writer writer = new StringWriter();

			final char[] buffer = new char[1024];
			try {
				final Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
				int n;
				while ((n = reader.read(buffer)) != -1) {
					writer.write(buffer, 0, n);
				}
			} finally {
				is.close();
			}
			return writer.toString();
		} else {
			return "";
		}
	}

	/**
	 * Displays the home page
	 * 
	 * @return The view for the home page
	 */
	@RequestMapping(value = "/ajax/find-address", method = RequestMethod.GET)
	public ModelAndView findAddress(HttpServletRequest request,
			@RequestParam(value = "address") String address) {
		// Set the http proxy to webcache.mydomain.com:8080

		// System.setProperty("http.proxyHost", "soproxy.bull.fr");
		// System.setProperty("http.proxyPort", "80");

		// Next connection will be through proxy.
		InputStream in = null;
		String out = "";
		try {
			final String urlString = "http://maps.googleapis.com/maps/api/geocode/json?address="
					+ URLEncoder.encode(address, "UTF-8") + "&sensor=false";
			final URL url = new URL(urlString);
			in = url.openStream();
			if (in != null) {
				out = convertStreamToString(in);
			}
		} catch (final Exception e) {
			throw new ApplicationException("Impossible to check the address " + address, e);
		}

		// From now on http connections will be done directly.
		final ModelAndView modelAndView = new ModelAndView("ajax/result");
		modelAndView.addObject("result", out);
		return modelAndView;
	}

	/**
	 * Displays the home page
	 * 
	 * @return The view for the home page
	 */
	@RequestMapping(value = "/ajax/find-professional", method = RequestMethod.GET)
	public ModelAndView findProfessional(HttpServletRequest request) {
		final LdapSearch ldapSearch = new LdapSearch();
		String json = "";
		final String rppsNumber = request.getParameter("rppsNumber");
		if (null != rppsNumber) {
			json = ldapSearch.getProfessionalByRppsAsJson(rppsNumber);
		} else {
			final String postalCode = request.getParameter("postalCode");
			final String adeliNumber = request.getParameter("adeliNumber");
			if (null != adeliNumber && null != postalCode) {
				json = ldapSearch.getProfessionalByAdeliAsJson(adeliNumber, postalCode);
			} else {
				json = "";
			}
		}
		final ModelAndView modelAndView = new ModelAndView("ajax/result");
		modelAndView.addObject("result", json);
		return modelAndView;
	}
}
